package com.lkm.app_ui.help;


import com.lkm.javatool.CollectionHelp;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;



public class PrinterMarketUtil {

    private static volatile DevicePrinter printer = null;
    private static String tailed = "";

    public static String getTailed() {
        return tailed;
    }

    public static void setTailed(String tailed) {
        tailed = tailed;
    }

    public static DevicePrinter getSingleton() {
        if (printer == null) {
            synchronized (DevicePrinter.class) {
                if (printer == null) {
                    printer = new DevicePrinter("/dev/ttyS1", 9600);


                }
            }
        }
        return printer;
    }

    public static void print(String content) {
        getSingleton().print(new StringBuilder(String.valueOf(content)).append(tailed).toString());
    }

    public static void printQRCode(String strCodedata, int nWidthX, int nVersion, int nErrorCorrectionLevel) {
        getSingleton().printQRCode(strCodedata, nWidthX, nVersion, nErrorCorrectionLevel);
    }

    public static void printQRCode(String strCodedata) {
        printQRCode(strCodedata, 6, 10, 4);
    }





    public static void print(String printTable, String qrString)throws Exception {
        byte[] str;


        int printTableLength= printTable.length();
        StringBuilder stringBuilder=new StringBuilder();
        int count=0;

        for (int i = 0; i < printTableLength; i++) {
            String it=printTable.substring(i,i+1);
            count+=  getCharWidth(it);
//                System.out.println(SaleFragment.getCharWidth(it)+"     "+it);
            stringBuilder.append(it);
            if(count>=32){
                count=0;
                stringBuilder.append("\n");
            }

        }
        String orderInfoStr="";


        BufferedReader br=new BufferedReader(new StringReader(stringBuilder.toString()));

        List<String> ss=new ArrayList<>();
        do {
            String s=null;
            try {
                s= br.readLine();
            }catch (Exception e){}

            if(s ==null)break;

            ss.add(s);
        }while (true);


        for(int i=ss.size()-1; i>=0;i-- ){
            orderInfoStr+=ss.get(i);
        }



        byte[] printContents=   new com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle.DevicePrinter().print(orderInfoStr);



//        System.out.println("====="+new Gson().toJson(str));;



//        System.out.println("====="+new Gson().toJson(str));;


        byte[] writeBytes=qrString.getBytes();

//        System.out.println("====="+new Gson().toJson(writeBytes));;





        str = new byte[]{0x1B,0x61,0x01,
                0x1D,0x77,0x03,
                0x1D,0x28,0x31,
                0x06,0x00,
                0x00,
                0x00};



        int j=writeBytes.length+2;
        str[9]= (byte) (j&0x00ff);
        str[10]= (byte) ((j>>8)&0x00ff);

        str= CollectionHelp.concatByte(str,writeBytes);





        byte[] printHead=new byte[]{0x1b,0x40};


        CollectionHelp.concatByte(printHead, new byte[]{0x1b,0x7b,0x01});




        CollectionHelp.concatByte(printHead,new byte[]{0x1b,0x33,0x00});

        printContents= CollectionHelp.concatByte( printHead,printContents);



        str=CollectionHelp.concatByte(str,printContents);



        str=CollectionHelp.concatByte(str,new byte[]{0x1b,0x4a,0x70} );
        getSingleton().write(str);
    }

    

    public static int getCharWidth(String strName) {
        int charWidth = 0;
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c)) {
                charWidth += 2;
            } else {
                charWidth += 1;
            }
        }
        return charWidth;
    }



    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }

}